package alkaid.eis.sys.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

import org.springframework.beans.BeanUtils;

import alkaid.eis.sys.core.AbstractBaseEntity;
import lombok.Getter;
import lombok.Setter;

/**
 * 地区
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@Entity
@Getter
@Setter
@Table(name = "eis_sys_area")
public class Area extends AbstractBaseEntity {
    public final static String PATH_SEP = "/";

    /**
     * 上级代码
     */
    @Column(updatable = false)
    private String pcode;

    /**
     * 代码
     */
    @Column(nullable = false, unique = true)
    private String code;

    /**
     * 名称
     */
    @Column(nullable = false)
    private String name;

    /**
     * 类型
     */
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private AreaType type;
    /**
     * code路径
     */
    private String pathCode;

    public Area() {
    }

    public Area(String code, String name, AreaType type) {
        this.code = code;
        this.name = name;
        this.type = type;
    }


    public Area(String pcode, String code, String name, AreaType type) {
        this.pcode = pcode;
        this.code = code;
        this.name = name;
        this.type = type;
    }

    @PrePersist
    @PreUpdate
    public void prePersist() {
        if (pcode != null) {
            pathCode = pcode + PATH_SEP + code;
        } else {
            pathCode = code;
        }
    }

    public void copy(Area source) {
        BeanUtils.copyProperties(source, this, "id,pathCode".split(","));
    }
}
